package com.test.pudamoney.util_pudamoney.http_pudamoney;

import static com.test.pudamoney.constant_pudamoney.Url_pudamoney.*;
import static com.test.pudamoney.util_pudamoney.http_pudamoney.HttpManager_pudamoney.Upload_Info_Url;

import android.text.TextUtils;

import com.test.pudamoney.activity_fragment_pudamoney.MainActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.ServiceFeedbackActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseInfoActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseLoginActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.info_pudamoney.InfoUploadActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.loan_pudamoney.AddPaystackBankAccountActivity_Niri_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.loan_pudamoney.LoanApplyActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.loan_pudamoney.LoanMainActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.loan_pudamoney.RepayPaystackActivity_Niri_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.login_pudamoney.LoginPhoneNumActivity_pudamoney;
import com.test.pudamoney.bean_pudamoney.config_info_pudamoney.AppShowInfo_pudamoney;
import com.test.pudamoney.bean_pudamoney.process_pudamoney.ProcessInfo_pudamoney;
import com.test.pudamoney.bean_pudamoney.user_info_pudamoney.LoanApplyDetailInfo_pudamoney;
import com.test.pudamoney.bean_pudamoney.user_info_pudamoney.LoanLimitInfo_pudamoney;
import com.test.pudamoney.bean_pudamoney.user_info_pudamoney.LoginInfo_pudamoney;
import com.test.pudamoney.bean_pudamoney.user_info_pudamoney.NiriBankAccountList_pudamoney;
import com.test.pudamoney.bean_pudamoney.user_info_pudamoney.RepayInfo_pudamoney;
import com.test.pudamoney.constant_pudamoney.Url_pudamoney;
import com.test.pudamoney.dialog_pudamoney.LoadingDialog_pudamoney;
import com.test.pudamoney.util_pudamoney.StringUtil_pudamoney;
import com.test.pudamoney.util_pudamoney.analytics_pudamoney.event_pudamoney.IAnalyticsEvent_pudamoney;
import com.test.pudamoney.util_pudamoney.analytics_pudamoney.util_pudamoney.AnalyticsUtil_pudamoney;

import org.json.JSONArray;
import org.json.JSONObject;

public class HttpPresenter_pudamoney {

    private static class Inner {
        private static final HttpPresenter_pudamoney instance = new HttpPresenter_pudamoney();
    }

    private HttpPresenter_pudamoney() {}

    public static HttpPresenter_pudamoney getInstance() {
        return Inner.instance;
    }

    private BaseActivity_pudamoney activity;

    public LoadingDialog_pudamoney progressBarDialog;

    public void setActivity(BaseActivity_pudamoney activity) {
        if (progressBarDialog != null && progressBarDialog.isShowing()) {
            progressBarDialog.dismiss();
        }
        if (activity != null) progressBarDialog = new LoadingDialog_pudamoney(activity);
        this.activity = activity;
    }

    public void progressBarDialogShow() {
        if (progressBarDialog == null) return;
        progressBarDialog.show();
    }

    public void progressBarDialogDismiss() {
        if (progressBarDialog == null) return;
        progressBarDialog.dismiss();
    }

    public void showToastOnUiThread(String text) {
        if (activity != null) {
            activity.showToastOnUiThread_pudamoney(text);
        }
    }

    //通知Activity处理响应
    public void notifyActivityHandleResponse(String word, JSONObject responseJson) {
        if (activity == null) return;
        try {
            String gcode = responseJson.optString("code");
            String gmsg = StringUtil_pudamoney.getSafeString(responseJson.optString("msg"));
            Object gobj = responseJson.opt("obj");

            //失败响应
            if (StringUtil_pudamoney.safeParseInt(gcode)!=0) {
                onResponseFail(word,gmsg);
                return;
            }

            //成功响应
            onResponseSuccess(word,gmsg,gobj);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //成功响应
    private void onResponseSuccess(String word,String gmsg,Object gobj){
        if (gobj instanceof JSONObject) {
            JSONObject objJsonObject = (JSONObject) gobj;
            if (word.equalsIgnoreCase(Upload_Info_Url)||(PATH_GLOBAL +word).equalsIgnoreCase(Upload_Info_Url)){
                if (activity instanceof BaseInfoActivity_pudamoney) ((BaseInfoActivity_pudamoney) activity).onInfoUploadResponse_pudamoney();
                return;
            }
            switch (word) {
                case PATH_APP_SHOW_INFO://获取配置信息
                    AppShowInfo_pudamoney.parse(objJsonObject);
                    if (activity instanceof MainActivity_pudamoney) ((MainActivity_pudamoney) activity).onAppShowInfoResponse();
                    else if (activity instanceof LoanMainActivity_pudamoney) ((LoanMainActivity_pudamoney) activity).onAppShowInfoResponse();
                    break;
                case PATH_SMS_CODE://获取验证码
                    if (activity instanceof BaseLoginActivity_pudamoney) ((BaseLoginActivity_pudamoney) activity).onRequestOtpCodeResponse_pudamoney(gmsg,objJsonObject);
                    break;
                case PATH_USER_LOGIN://用户登录
                    LoginInfo_pudamoney.parse(objJsonObject);
                    if (activity instanceof BaseLoginActivity_pudamoney) ((BaseLoginActivity_pudamoney) activity).onUserLoginResponse_pudamoney();
                    break;
                case PATH_FEED_BACK://提交反馈
                    if (activity instanceof ServiceFeedbackActivity_pudamoney) ((ServiceFeedbackActivity_pudamoney) activity).onSubmitServiceFeedback();
                    break;
                case PATH_USER_PROCESS://用户流程状态
                    ProcessInfo_pudamoney.parse(objJsonObject.toString());
                    activity.onUserProcessResponse_pudamoney();
                    break;
                case PATH_LOAN_LIMIT://额度信息
                    LoanLimitInfo_pudamoney.parse(objJsonObject);
                    if (activity instanceof LoanMainActivity_pudamoney) ((LoanMainActivity_pudamoney) activity).onLoanLimitInfoResponse();
                    break;
                case PATH_GET_LOAN_APPLY_DETAIL://借款页信息
                    LoanApplyDetailInfo_pudamoney.parse(objJsonObject);
                    if (activity instanceof LoanMainActivity_pudamoney) ((LoanMainActivity_pudamoney) activity).onLoanApplyDetailResponse();
                    else if (activity instanceof BaseInfoActivity_pudamoney) ((BaseInfoActivity_pudamoney) activity).onLoanApplyDetailResponse_pudamoney();
                    else if (activity instanceof LoanApplyActivity_pudamoney) ((LoanApplyActivity_pudamoney) activity).onLoanApplyDetailResponse();
                    break;
                case PATH_LOAN_APPLY://发起借款申请响应
//                        AFInstallHelper.getInstance().saveEventInfo(AFInstallHelper.AFEvent.AT_LOAN_APPLY);
//                        FirebaseHelper.getInstance().saveEventInfo(AFInstallHelper.AFEvent.AT_LOAN_APPLY);
                    AnalyticsUtil_pudamoney.INSTANCE.addEvent(IAnalyticsEvent_pudamoney.AT_LOAN_APPLY);
                    if (activity instanceof LoanApplyActivity_pudamoney) ((LoanApplyActivity_pudamoney) activity).onLoanApplyResponse();
                    break;
                case PATH_REPAY_INFO://还款信息展示
                    RepayInfo_pudamoney.parse(objJsonObject);
                    if (activity instanceof LoanMainActivity_pudamoney) ((LoanMainActivity_pudamoney) activity).onRepayDetailResponse();
                    break;
                case PATH_ORDER_REPAY_SDK://线上还款-初始化paystack订单（尼日）
                    if (activity instanceof AddPaystackBankAccountActivity_Niri_pudamoney) ((AddPaystackBankAccountActivity_Niri_pudamoney) activity).onOrderRepaySDKResponse(objJsonObject);
                    else if (activity instanceof RepayPaystackActivity_Niri_pudamoney) ((RepayPaystackActivity_Niri_pudamoney) activity).onOrderRepaySDKResponse(objJsonObject);
                    break;
                case PATH_CREATE_VIRTUAL_ACCOUNT://线下还款-创建虚拟账号还款（尼日）
                    if (activity instanceof LoanMainActivity_pudamoney) ((LoanMainActivity_pudamoney) activity).onInitOfflinePayChannelResponse();
                    break;
                case PATH_UPLOAD_IMAGE_FILE://图片上传响应
                    String url = objJsonObject.optString("url");
                    break;
                default:
                    //throw new IllegalStateException("Unexpected value: " + word);
            }
        } else if (gobj instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) gobj;
            switch (word) {
                case PATH_FETCH_BANK_CODE://获取银行列表（尼日）
                    if (activity instanceof InfoUploadActivity_pudamoney) ((InfoUploadActivity_pudamoney) activity).onFetchBankCodeResponse(jsonArray);
                    break;
                case PATH_BANK_LIST://获取paystack卡列表
                    NiriBankAccountList_pudamoney.parse(jsonArray);
                    if (activity instanceof RepayPaystackActivity_Niri_pudamoney) ((RepayPaystackActivity_Niri_pudamoney) activity).onFetchBankListResponse();
                    else if (activity instanceof LoanMainActivity_pudamoney) ((LoanMainActivity_pudamoney) activity).onFetchBankListResponse_Niri();
                    break;
            }
        } else {
            //其它类型
            gmsg = StringUtil_pudamoney.getSafeString(gmsg);
            if (TextUtils.isEmpty(gmsg)) {
                gmsg = "Response something error";
            }
            activity.showToast_pudamoney(gmsg);
        }
    }

    //失败响应
    private void onResponseFail(String word,String gmsg){
        activity.showToast_pudamoney(gmsg);
//        switch (word){
//
//        }
    }

}
